package cn.cit.training.feignclient.controller;

import cn.cit.training.feignclient.feign.CitDataFeign;
import cn.cit.training.feignclient.vo.CommonResult;
import cn.cit.training.feignclient.entity.CitData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * class mode
 *
 */
@Slf4j
@RestController
@RequestMapping
@Api(description = "data-API")
public class FeignController {

    @Autowired
    private CitDataFeign citDataFeign;

    /**
     * status
     *
     * @return
     */
    @ApiOperation("检查状态")
    @GetMapping("/")
    public CommonResult status() {
       return citDataFeign.status();
    }

    /**
     * 查询user列表
     *
     * @param pageSize 每页记录数
     * @param currentPage 当前页码
     * @param field 查询字段
     * @param min 区间最小
     * @param max 区间最大
     *
     * @return
     */
    @ApiOperation("查询列表")
    @GetMapping("/citDatas")
    public CommonResult getUserPage(
            @RequestParam(value = "pageSize", required = false, defaultValue = "10")
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "currentPage", required = false, defaultValue = "1")
            @ApiParam(value = "当前页码", required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(value = "field", required = false, defaultValue = "")
            @ApiParam(value = "查询字段：year, t_travel, t_duration", required = false ,example = "t_duration") String field,
            @RequestParam(value = "min", required = false, defaultValue = "0")
            @ApiParam(value = "区间最小", required = false, defaultValue = "0" ,example = "1971") Integer min,
            @RequestParam(value = "max", required = false, defaultValue = "0")
            @ApiParam(value = "区间最大", required = false, defaultValue = "0" ,example = "1980") Integer max) {
        return citDataFeign.getCitDataPage(pageSize,currentPage,field,min,max);
    }

    /**
     * 查询user
     *
     * @param id 唯一主键 id
     * @return
     */
    @ApiOperation("通过id查询单个数据")
    @GetMapping("/citDatas/{id}")
    public CommonResult getCitDataById(@PathVariable("id") @ApiParam(value = "主键id", required = true)  Integer id) {
       return citDataFeign.getCitDataById(id);
    }

    /**
     * 添加CitData
     *
     * @param citData
     * @return
     */
    @ApiOperation("添加数据")
    @PostMapping("/citDatas")
    public CommonResult createCitData(@RequestBody  @ApiParam(value = "数据实体表，不用生成主键, id=null", required = true) CitData citData) {
        return citDataFeign.createCitData(citData);
    }

    /**
     * 删除CitData
     *
     * @param id
     * @return
     */
    @ApiOperation("删除数据")
    @DeleteMapping("/citDatas/{id}")
    public CommonResult deleteCitData(@PathVariable("id") @ApiParam(value = "主键id", required = true) Integer id) {
        return citDataFeign.deleteCitData(id);
    }

    /**
     * 更新CitData
     *
     * @param id
     * @param citData
     * @return
     */
    @ApiOperation("更新数据")
    @PutMapping("/citDatas/{id}")
    public CommonResult updateCitData(@PathVariable("id") @ApiParam(value = "主键id，例 id=5", required = true) Integer id,
                                        @RequestBody @ApiParam(value = "数据实体表，可以带主键id,也可以不带，例id=null", required = true) CitData citData) {
        return citDataFeign.updateCitData(id,citData);
    }


}
